const mongoose = require('mongoose')
const { ObjectId } = mongoose.Types
const { Message, Feedback } = require('../../schema')

mongoose.connect('mongodb://127.0.0.1:27017/sportswear').then()
const messageCollection = mongoose.model('message', Message)
const feedbackCollection = mongoose.model('feedback', Feedback)
const characters = ['jack', 'jodi', 'jai', 'jon', 'jazebelle', 'jed', 'joe', 'jean', 'jake', 'jordan', 'james', 'jia', 'josephine', 'julie', 'jana']

function insertMsg (title, description, html) {
    return new Promise(resolve => {
        const record = new messageCollection({
            msgid: ObjectId(),
            title,
            description,
            avatar: `https://joeschmoe.io/api/v1/${characters[Math.floor(Math.random() * 15)]}`,
            html,
            readIt: [],
            time: new Date(new Date().getTime() + 8 * 60 * 60 * 1000)
        })
        record.save().then(res => {
            resolve('消息添加成功')
        }).catch(err => {
            resolve('出错了')
        })
    })
}

function getMsg () {
    return new Promise(resolve => {
        messageCollection.find({}).sort({ time: -1 }).then(res => {
            resolve(res)
        })
    })
}

// 获取未读信息[显示]
function getReadMsg (userid) {
    return new Promise(resolve => {
        messageCollection.find({ readIt: { $nin: userid } }).sort({ time: -1 }).then(res => {
            resolve(res)
        })
    })
}

// 获取已读信息[不显示]
function getReadList (userid) {
    return new Promise(resolve => {
        messageCollection.find({ readIt: { $in: userid } }).sort({ time: -1 }).then(res => {
            resolve(res)
        })
    })
}

// 获取读取getReadListCount
function getReadListCount (userid) {
    return new Promise(resolve => {
        messageCollection.find({ readIt: { $nin: userid } }).count(1).then(res => {
            resolve(res)
        })
    })
}

function removeMsg (msgid) {
    return new Promise(resolve => {
        messageCollection.deleteOne({
            msgid
        }).then(res => {
            if (res.deletedCount) {
                resolve('消息删除成功')
            } else {
                resolve('消息已经删除')
            }
        })
    })
}

function modifyReadIt (msgid, userid) {
    return new Promise(resolve => {
        messageCollection.updateOne({
            msgid
        }, {
            $push: {
                readIt: userid
            }
        }).then(res => {
            if (res.modifiedCount) {
                resolve('标记为已读')
            } else {
                resolve('标记未发生改变')
            }
        })
    })
}

function insertFeedback (userid, msg) {
    return new Promise(resolve => {
        const record = new feedbackCollection({ feedback: ObjectId(), userid, msg })
        record.save().then(res => {
            resolve('感谢您的反馈成功')
        }).catch(err => {
            resolve('服务器繁忙，请稍后再试')
        })
    })
}

function getFeedback () {
    return new Promise(resolve => {
        feedbackCollection.find({}).then(res => {
            resolve(res)
        })
    })
}

module.exports = {
    insertMsg,
    getMsg,
    getReadMsg,
    getReadList,
    getReadListCount,
    modifyReadIt,
    removeMsg,
    getFeedback,
    insertFeedback,
}
